﻿package common.components.transition.transitions
{
	
	import common.components.transition.events.TransitionEvent;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import mx.controls.Image;
	import mx.core.Application;
	import mx.effects.Fade;
	import mx.events.EffectEvent;
	
	// in and out
	public class Transition extends EventDispatcher {
	
		protected var transTime : Number;
		
		protected var stayTime : Number;
		
	    public var image:Image;
	    
	    private var runner:TransitionRunner;
		
		private var stayTimer:Timer = null;
				
		public function Transition(runner:TransitionRunner,img:Image,transTime:Number,stayTime:Number)
		{
			this.runner = runner;
			this.image = img;
			this.transTime = transTime;
			this.stayTime = stayTime;
		}
	
		public function walkIn():void
		{
			this.transInStart();
		}
				
		protected function transInStart():void
		{
			this.addEventListener(TransitionEvent.TRANS_IN_END,this.onTransInEnd);
			this.image.visible = true;
			this.image.alpha = 1;
		}
	
		protected function onTransInEnd(evt:TransitionEvent):void
		{
			this.removeEventListener(TransitionEvent.TRANS_IN_END,this.onTransInEnd);
			this.stayStart();
		}
		
		protected function stayStart():void
		{
			this.dispatchEvent(new TransitionEvent(TransitionEvent.TRANS_STAY_START));	
			
			if ( this.runner.auto )
			{
				trace( "stay time: ", stayTime );
				this.stayTimer = new Timer(this.stayTime*1000,1);
				stayTimer.addEventListener(TimerEvent.TIMER_COMPLETE,this.autoGo);
				stayTimer.start();
			}
		}
		
		public function cutStay():Boolean
		{
			if ( this.stayTimer )
			{
				this.stayTimer.removeEventListener(TimerEvent.TIMER_COMPLETE,this.autoGo);
				this.stayTimer = null;
				return true;
			}			
			else
				return false;
		}
				
		private function autoGo(evt:Event):void
		{
			trace("autoGo");
			this.stayTimer = null;
			this.runner.go(true,true);
		}

		public function walkOut():void
		{
			//-arming add
			this.removeEventListener(TransitionEvent.TRANS_IN_END,this.onTransInEnd);
			cutStay();
			//-arming add
			
			this.stayTimer = null;
			this.onStayEnd();
		}
		
		protected function onStayEnd():void
		{
			this.dispatchEvent(new TransitionEvent(TransitionEvent.TRANS_STAY_END));	
			this.transOutStart();
		}
		private var fade:Fade;
		protected function transOutStart():void
		{
			fade = new Fade(this.image);
			fade.alphaFrom = 1;
			fade.alphaTo = 0;
			fade.duration = this.transTime*1500;
			
			if(Application.application.url.indexOf('backadmin') > 0)
			{
				fade.duration = 100;
			}
			
			fade.addEventListener(EffectEvent.EFFECT_END, onTransOutEnd);
			fade.play();
			this.dispatchEvent(new TransitionEvent(TransitionEvent.TRANS_OUT_START));	
		}
		
		protected function onTransOutEnd(evt:EffectEvent):void
		{
			this.dispatchEvent(new TransitionEvent(TransitionEvent.TRANS_OUT_END));	
		}	
		
		//-------arming add:
		public function cutWalkOut():void
		{
			fade.stop();
			fade.removeEventListener(EffectEvent.EFFECT_END, onTransOutEnd);
		}
	}

}